<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends Admin_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->loadData('controllerName',$this->router->fetch_class());
	}
	
	public function index(){
		redirect('administrator/user/manager');
	}
	
	//Quan ly người dùng
	public function manager(){
		$per_page = 20;
		$num_link = 7;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->loadScript(base_url('js/admin/jquery.modify.js'));
		$this->loadScript(base_url('js/admin/jquery.update.status.js'));
		$this->headTitle('Quản lý người dùng - Admin Panel');
		
		$currentPage = $this->input->get('page',true);
		$currentPage = (!empty($currentPage))?$currentPage:'1';
		$this->load->model('Users_Model');
		$countusers = $this->Users_Model->countUsers();
		$this->loadData('countusers',$countusers);
		$pagingConfig = $this->init_pagination('administrator/user/manager?act=paging', $countusers, $per_page, $num_link);
		$this->loadData('paging', $this->pagination->create_links());
		$this->loadData('listusers', $this->Users_Model->getUsersLimit((($currentPage-1) * $pagingConfig['per_page']), $pagingConfig['per_page']));
			
		if($this->input->post()){
			if($this->form_validation->run('formsearch') == true){
				$keysearch = trim($this->input->post('keysearch', true));
				$fieldsearch = trim($this->input->post('field_search', true));
				$this->loadData('textkeysearch', $keysearch);
				$this->loadData('paging','');
				$this->loadData('listusers', $this->Users_Model->searchUser($keysearch, $fieldsearch));
			}

		}
		$this->loadView('administrator/user/manager');
	
	}
	//Them kenh truyen hinh
	public function add(){
		$this->load->helper('form');
		$this->load->library('form_validation');	
		$this->load->model('Users_Model');
		if($this->input->post()){
			if($this->form_validation->run('user_add') == true){
				if($this->Users_Model->checkExist('UserName') && $this->Users_Model->checkExist('Email')){					
					$username = trim($this->input->post('UserName', true));
					$pwd = $this->randomCode(6);
					$subject = "Thông báo cấp phát tài khoản của Truyền hình cáp Đồng Tháp";
					$message = "Tên tài khoản: ".$username."<br/>";
					$message .= "Mật khẩu: ".$pwd;
					if($this->sendMail($this->input->post('Email'), $subject, $message)){
						$this->Users_Model->insertUser($pwd);
						redirect('administrator/user/manager');	
					}
				}else{
					if(!$this->Users_Model->checkExist('UserName')){
						$error_username = "<p>Tên tài khoản đã tồn tại. Xin nhập vào tên tài khoản khác.</p>";
						$this->loadData('error_username', $error_username);
					}
					if(!$this->Users_Model->checkExist('Email')){
						$error_email = "<p>Email này đã được sử dụng. Xin nhập vào email khác.</p>";
						$this->loadData('error_email', $error_email);
					}
				}
			}		
		}
		$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
		$this->headTitle('Thêm người dùng - Admin Panel');
		$this->loadView('administrator/user/add');

	}
	
	//Sua nguoi dung
	public function modify(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->load->model('Users_Model');	
		$userid = $this->input->get('userid', true);
		$user = $this->Users_Model->getUserByUserId($userid);
		if($user){
			if($this->input->post()){
				if($this->form_validation->run('user_modify') == true){		
					if($this->Users_Model->checkExist('Email') || $this->input->post('Email',true) == $user->Email){				
						$this->Users_Model->updateUser($userid);
						redirect('administrator/user/manager');
					}else{
						$error_email = "<p>Email này đã được sử dụng. Xin nhập vào email khác.</p>";
						$this->loadData('error_email', $error_email);
					}
				}		
			}
			$this->loadData('user', $user);
			$this->loadScript(base_url('js/admin/jquery.fixedListcontrol.js'));
			$this->headTitle('Sửa người dùng - Admin Panel');
			$this->loadView('administrator/user/modify');
		}else{
			redirect('administrator/user/manager');
		}
	}
	//Doi trang thai nguoi dung
	public function change_status(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$status = $this->input->post('status', true);
			$this->load->model('Users_Model');
			$changeStatus = $this->Users_Model->changeStatusUser($key, $status);
			if($changeStatus){
				$json['status'] = $changeStatus;
				$json['success'] = true;
			}else{
				$json['success'] = false;
			}
			echo json_encode($json);
		}
	}
	
	//Xoa nguoi dung
	public function delete(){
		if($this->input->post()){
			$key = $this->input->post('key', true);
			$this->load->model('Users_Model');		
			if($this->Users_Model->deleteUser($key)){
				$json["success"] = true;
			}else{
				$json["success"] = false;
			}
			echo json_encode($json);
		}
	}
	
}